import os
import sys
from pathlib import Path
import pandas as pd
import numpy as np
import csv

"""未实现

pd.merge()#合并2个df-》合并多个df
"""
def print_var_len_shape(v):
    """
    功能：变量类型与形状打印
    调用：print_var_len_shape(1)
    """
    if type(v) in [list,dict,tuple,str]:#len()形状类型
        print(f"{type(v)},len={len(v)}")
    elif type(v) in [int,float]:#无len()和shape
        print(f"{type(v)},{v}")
    else:
        print(f"{type(v)},shape={v.shape}")
    return type(v)


def csv2DataFrame(foldername,filename,sep=',',header='infer', usecols=[0]):
    """功能：读取（当前绝对路径下-指定文件夹下）csv文件->DataFrame对象；
    参数：header是列名(0表示使用首行当列名)，usecols是使用的列数
    调用：
    foldername, filename='csv_data','test_seen.csv'
    data=csv2DataFrame(foldername, filename)
    print_var_len_shape(data)
    """
    # filename='test_seen.csv'
    save_dir = Path(__file__).parent.absolute()  ##取绝对目录-当前文件所在目录
    filepath = os.path.join(save_dir,foldername, filename)#
    # print(filepath)
    df = pd.read_csv(filepath, sep=sep,header=header,usecols=usecols)
    return df



def df2xy(df):
    """DataFrame->x,y（均为array）
    调用：
    foldername, filename='csv_data','test_seen.csv'
    df=csv2DataFrame(foldername, filename,header=0, usecols=[0,1])
    x,y=df2xy(df)
    print_var_len_shape(x)#<class 'numpy.ndarray'>,shape=(5400, 1)
    print_var_len_shape(y)#<class 'numpy.ndarray'>,shape=(5400,)
    """
    print_var_len_shape(df)
    col_num = df.shape[1]
    row_num = df.shape[0]
    x=df.iloc[:, 0:col_num -1]#df的左闭右闭#<class 'pandas.core.frame.dfFrame'>,shape=(5400, 1)
    # # print(x)
    y=df.iloc[:,col_num - 1]#<class 'pandas.core.series.Series'>,shape=(5400,)
    # # DataFrame没有tolist()方法，而series.Series有tolist()方法
    x = np.array(x)  # df->array
    y = np.array(y)  # df->array
    # x=np.array(x).tolist()#df->array->list
    # y=np.array(y).tolist()#df->array->list
    return x,y


def df2txt(df,outname,sep):
    """DataFrame对象->.txt
    调用：
    outname='df.txt'
    foldername, filename='csv_data','test_seen.csv'
    data=csv2DataFrame(foldername, filename)
    print_var_len_shape(data)
    """
    df.to_csv(outname,sep='\t',header=False,index=False)#df->.txt,不要列名，不要行索引


def csv_group2DataFrame(foldername,filelist,sep=',',header='infer', usecols=[0]):
    """功能：csv文件列表（当前绝对路径下-指定文件夹下）->DataFrame对象（按行拼接）；
    参数：header是列名（无列名时header=None），，usecols是使用的列数
    调用：
    foldername='csv_data'
    filelist=['test_seen.csv','test_unseen.csv','train_seen.csv']
    df_merged=csv_group2DataFrame(foldername,filelist)
    print_var_len_shape(df_merged)
    df_merged.to_csv('df.txt',header=False,index=False)#df->.txt,不要列名，不要行索引
    """
    # filename='test_seen.csv'
    save_dir = Path(__file__).parent.absolute()  ##取绝对目录-当前文件所在目录
    df_groups = []
    for filename in filelist:
        filepath = os.path.join(save_dir,foldername, filename)#
        # print(filepath)
        df = pd.read_csv(filepath, sep=sep,header=header,usecols=usecols)
        # merge any number of dataframes
        from functools import reduce
        df_groups.append(df)
        # print_var_len_shape(df)
    df_merged =pd.concat(df_groups)# [df]->df
    return df_merged


foldername, filename='csv_data','test_seen.csv'
df=csv2DataFrame(foldername, filename,header=0, usecols=[0,1])